<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // October 11, 2011
    // IN PARAMETERS : action (Send, Approve, Disapprove, Reject, Archive, Cancel, Send (Cancelled))
    // 
    // This AJAX Request records the receiving action to the database.
    // This is used in viewPageReceiving.php
 
// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    //LOAD PRE-DEFINED VARIABLES FOR DATABASE ACCESS
//    include('../includes/siteConfig.php');
     
    
    $hostName = "localhost";
     $rootName = "dbasegr";
     $dBasePassword = "dbgr2012";
    $dBaseName = 'greenroute';
 
 
 
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    // GET VARIABLES VIA POST METHOD
    $action = $_POST['action'];
    $documentId = $_POST['documentId'];
    $selectedSubRouteId = $_POST['selectedSubRouteId']; // THIS IS STRING.
    $currentSubRouteId = $_POST['currentSubRouteId'];
    
    $attachedCount = $_POST['attachedCount'];
    $detachedCount = $_POST['detachedCount'];
    $addedCount = $_POST['addedCount'];
    $mergedCount = $_POST['mergedCount'];
    
    $attachedString = $_POST['attachedString'];
    $detachedString = $_POST['detachedString'];
    $addedString = $_POST['addedString'];
    $addedDocSetupIdString = $_POST['addedDocSetupIdString'];
    $mergedString = $_POST['mergedString'];
    
    $message = $_POST['message'];
   
    // GET LOGGED USER ID IN SESSION
    $loggedUserId = $_SESSION['loggedUserId'];

    //GET THE CURRENT DATE TIME
    date_default_timezone_set('Asia/Singapore');
    $currentDateTime= date("Y-m-d H:i:s" , time());
    
    //GET CURRENT STEP ID BASED ON MAIN DOCUMENT.
    $query = 
        "
            SELECT *
            FROM MainDocument, RouteStep
            WHERE MainDocument.currentStepId = RouteStep.stepId AND MainDocument.mainDocumentId = '$documentId'
        ";
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    
    $currentStepId = $row['currentStepId'];
    $stepNumber = $row['stepNumber'];
    $nextStepNumber = $stepNumber + 1;
    $routeId = $row['routeId'];
    error_log("ROUTE ID>>>>>>>>>>>>> $routeId");
    
    //GET PREVIOUS STEP ID BASED ON STEP NUMBER, ROUTE ID, and SUB ROUTE
    $prevStepNumber = $stepNumber - 1;
    
    // GET PREVIOUS STEP ID (QUERY FOR RouteId = NOT NULL)
    if ($selectedSubRouteId == "NULL") {
        $query = 
            "
                SELECT stepId
                FROM RouteStep
                WHERE stepNumber = '$prevStepNumber' AND routeId = '$routeId' AND subRouteId is NULL
            ";

    }

    // GET PREVIOUS STEP ID (QUERY FOR RouteId = NOT NULL)
    else {
        $query = 
            "
                SELECT stepId
                FROM RouteStep
                WHERE stepNumber = '$prevStepNumber' AND routeId = '$routeId' AND subRouteId = '$selectedSubRouteId'
            ";
    }
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    $prevStepId = $row['stepId'];
    
    
    // SUCCEEDING CODE : IDENTIFIES THE DETACHED, ADDED, AND MERGED ATTACHMENTS 
    // THEN CREATE ITS CORRESPONDING LOGS AND RECORD(if necessary, for ADDED)
    
    // VERIFY IF THERE ARE DETACHED ATTACHMENTS, IF YES, CREATE LOGS
    if ($detachedCount > 0) {
        $detachedDocuments = explode(",", $detachedString);

        for ($x=0; $x< $detachedCount;$x++) {
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$detachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Detached')
                ";
            mysql_query($query);
        }
    }    

    // VERIFY IF THERE ARE ADDED ATTACHMENTS, IF YES, CREATE RECORD AND LOGS
    if ($addedCount > 0) {
        // GET MAIN LABEL ID OF MAIN DOCUMENT
        $query = 
            "
                SELECT labelId
                FROM Document_Label
                WHERE documentId = '$documentId' AND mainLabel = '1'
            ";
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        $row = mysql_fetch_array($result);
        $mainDocLabelId = $row['labelId'];
        
        
        // GET ALL NEWLY ADDED ATTACHMENTS
        $attachedCount += $addedCount;
        $attachedString = $attachedString . $addedString;
        
        $addedDocuments = explode(",", $addedString);
        $addedDocSetupIds = explode(",", $addedDocSetupIdString);

        for ($x=0; $x< $addedCount;$x++) {
            // CREATE RECORD OF ATTACHMENT IN ATTACHMENT TABLE.
            $query = 
                "
                    INSERT INTO Attachment (attachmentId, mainDocumentId, documentSetupId, location, status)
                    VALUES ('$addedDocuments[$x]', '$documentId', '$addedDocSetupIds[$x]', '$loggedUserId', 'Active')
                ";
            mysql_query($query);
            
            // CREATE LOG
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$addedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Tagged')
                ";
            mysql_query($query);
            
            // CREATE LABEL FOR NEWLY ADDED ATTACHMENTS
            $query = 
                "
                    INSERT INTO Document_Label (documentId, labelId, mainLabel)
                    VALUES ('$addedDocuments[$x]', '$mainDocLabelId', '0')
                ";
            mysql_query($query);
            
        }
    }    

    // VERIFY IF THERE ARE MERGED ATTACHMENTS, IF YES, CREATE LOGS
    if ($mergedCount > 0) {
        $attachedCount += $mergedCount;
        $attachedString = $attachedString . $mergedString;
        
        
        $mergedDocuments = explode(",", $mergedString);

        for ($x=0; $x< $mergedCount;$x++) {
            
            // CHANGES THE mainDocumentId of the attachment in Attachment table.
            $query = 
                "
                    UPDATE Attachment
                    SET mainDocumentId = '$documentId'
                    WHERE attachmentId = '$mergedDocuments[$x]'
                ";
            mysql_query($query);
            
            // SETS THE RECEIVE FLAG OF THE SENT ATTACHMENT TO MERGED.
            $query = 
                "
                    UPDATE Sent_Attachment
                    SET receiveFlag = 'Merged'
                    WHERE documentId = '$mergedDocuments[$x]'
                ";
            mysql_query($query);
            
            // CREATE LOG OF MERGED.
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$mergedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Merged')
                ";
            mysql_query($query);
        }
    }  
    
    switch($action) {
        
        /**************************** SEND AND APPROVE (START) *********************************/           
            
        
        /*
        ALGORITHM :
                if case = Approve, create a record of Approval in Log Table.
            nextStepId = Get next stepId
            Change currentStepId of Main Document from MainDocument table to : $nextStepId
            Change action of Main Document from MainDocument table to : Sent
            Create a log for the sending process. (Sending of Main Document and its Attachments)
        */
        
        case "Approve" :
            
            // IF CASE = APPROVE, CREATE A RECORD OF APPROVAL IN LOG TABLE.
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Approved')
                ";
            mysql_query($query);
            
        case "Send" :    
            
            // GET NEXT STEP ID (QUERY FOR RouteId = NULL)
            if ($selectedSubRouteId == "NULL") {
                $query = 
                    "
                        SELECT stepId
                        FROM RouteStep
                        WHERE stepNumber = '$nextStepNumber' AND routeId = '$routeId' AND subRouteId is NULL
                    ";
                
            }
            
            // GET NEXT STEP ID (QUERY FOR RouteId = NOT NULL)
            else {
                $query = 
                    "
                        SELECT stepId
                        FROM RouteStep
                        WHERE stepNumber = '$nextStepNumber' AND routeId = '$routeId' AND subRouteId = '$selectedSubRouteId'
                    ";
            }
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $nextStepId = $row['stepId'];
            
            // CHANGES THE currentStepId and action attributes of MainDocument TABLE.
            $query = 
                "
                    UPDATE MainDocument
                    SET currentStepId = '$nextStepId', action = 'Sent'
                    WHERE mainDocumentId = '$documentId'
                ";
            mysql_query($query);
            
            // CREATE LOG RECORD OF MAIN DOCUMENT
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                ";
            mysql_query($query);
            
            // VERIFY IF THERE ARE ATTACHED ATTACHMENTS, IF YES, CREATE LOGS
            if ($attachedCount > 0) {
                $attachedDocuments = explode(",", $attachedString);

                for ($x=0; $x< $attachedCount;$x++) {
                    $query = 
                        "
                            INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                            VALUES ('$attachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                        ";
                    mysql_query($query);
                }
            }
            
            break;
            
            // ############################ PENDING IN SEND AND APPROVE ###################
            // Kulang pa ng saving sa Logs of Main Document and attachments.
            
        /**************************** SEND AND APPROVE (END) *********************************/
        
            
            
            
            
            
            
        /**************************** RETURN FOR REVISIONS (START) ************************************/
        /* 
         * ALGORITHM FOR RETURN FOR REVISIONS : 
         * 
         * MainDocument table ==>>> currentStepId = $prevStepId, action = 'Sent'
         * Create Logs 'Returned for Revisions' for Main Document
         * $disapprovalLogId = Get Disapproval LogId of Main Document from Log table.
         * Save Disapproval Message to LogMessage table ==>> logId = $disapprovalLogId, message = $message (FROM POST) 
         * Create Logs 'Sent' for Main Document and Attachments.
         
         */
        case "Return For Revisions" : 
            
            // CHANGES THE currentStepId and action attributes of MainDocument TABLE.
            $query = 
                "
                    UPDATE MainDocument
                    SET currentStepId = '$prevStepId', action = 'Sent'
                    WHERE mainDocumentId = '$documentId'
                ";
            mysql_query($query);
            
            // CREATE LOG FOR MAIN DOCUMENT ("Returned for Revisions").
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Returned For Revisions')
                ";
            mysql_query($query);
            
            // GET LOGID OF DISAPPROVAL
            $query = 
                    "
                        SELECT logId
                        FROM Log
                        WHERE activity = 'Returned For Revisions' AND documentId = '$documentId'
                        ORDER BY logId DESC
                    ";
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $disapprovalLogId = $row['logId'];
            
            // THIS QUERY SAVES THE DISAPPROVAL MESSAGE TO LogMessage Table.
            $query = 
                "
                    INSERT INTO LogMessage (logId, message)
                    VALUES ('$disapprovalLogId', '$message')
                ";
            mysql_query($query);
            
            // CREATE LOG RECORD OF MAIN DOCUMENT
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                ";
            mysql_query($query);
            
            // VERIFY IF THERE ARE ATTACHED ATTACHMENTS, IF YES, CREATE LOGS
            if ($attachedCount > 0) {
                $attachedDocuments = explode(",", $attachedString);

                for ($x=0; $x< $attachedCount;$x++) {
                    $query = 
                        "
                            INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                            VALUES ('$attachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                        ";
                    mysql_query($query);
                }
            }
            
            break;
       /**************************** RETURN FOR REVISIONS (END) ************************************/
        
        
        
        
        
        
        
        
        
        
        /**************************** REJECT/DISAPPROVE (START) ************************************/
        /*
         * ALGORITHM FOR REJECT/DISAPPROVE :
         * 
         * Get $rejectStepId = where StepNumber = 1, subRouteId = 'REJECTTRAC'
         * MainDocument table : currentStepId = $rejectStepId, action = 'Sent'
         * Create Logs "Rejected" of Main Document
         * Get latest logId. Save Reject message to LogMessage table.
         * Create Logs "Sent" of Main Document and Attachments.
         * 
         */
        case "Disapprove" :
            
            // GET $rejectStepId. Document Route follows the subRoute REJECTTRAC.
            $query = 
                "
                    SELECT stepId
                    FROM RouteStep
                    WHERE stepNumber = '1' AND routeId = '$routeId' AND subRouteId = 'REJECTTRAC'
                ";

            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $rejectStepId = $row['stepId'];
            
            // UPDATE MAIN DOCUMENT TABLE.
            $query = 
                "
                    UPDATE MainDocument
                    SET currentStepId = '$rejectStepId', action = 'Sent'
                    WHERE mainDocumentId = '$documentId'
                ";
            mysql_query($query);
            
            // CREATE LOG FOR MAIN DOCUMENT ("Reject").
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Rejected')
                ";
            mysql_query($query);
            
            // GET LOGID OF LATEST REJECT
            $query = 
                    "
                        SELECT logId
                        FROM Log
                        WHERE activity = 'Rejected' AND documentId = '$documentId'
                        ORDER BY logId DESC
                    ";
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $rejectLogId = $row['logId'];
            
            // THIS QUERY SAVES THE REJECTION MESSAGE TO LogMessage Table.
            $query = 
                "
                    INSERT INTO LogMessage (logId, message)
                    VALUES ('$rejectLogId', '$message')
                ";
            mysql_query($query);
            
            // CREATE LOG RECORD OF MAIN DOCUMENT
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                ";
            mysql_query($query);
            
            // VERIFY IF THERE ARE ATTACHED ATTACHMENTS, IF YES, CREATE LOGS
            if ($attachedCount > 0) {
                $attachedDocuments = explode(",", $attachedString);

                for ($x=0; $x< $attachedCount;$x++) {
                    $query = 
                        "
                            INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                            VALUES ('$attachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                        ";
                    mysql_query($query);
                }
            }
            
            break;
        
        /**************************** REJECT/DISAPPROVE (END) ************************************/
        
        
        
        
        
        
        
        
        
        
        
        
        
        /**************************** CANCEL (START) ************************************/
        /*
         * ALGORITHM FOR CANCEL
         * Get current holder of the main document.
         * 
         * if (currentHolder == loggedUser) {
         *    get $cancelStepId : subRoute = 'CANCELTRAC', stepNumber = 1;
         * 
         *    MainDocument table : activity = 'Sent'
         *                         currentStepId = '$cancelStepId'
         * 
         *    Log table : Cancelled (Main Document)
         *                Sent (Main Document)
         *                Sent (Attachments) 
         * }
         * 
         * // currentHolder of the documents is not the logged user / origin
         * 
         * else {
         *    MainDocument table : activity = 'Cancelled'
         *    Log table : Cancelled (Main Document)
         * } 
         */
        case "Cancel Transaction" : 
            // GET CURRENT HOLDER (USER ID) of MAIN DOCUMENT.
            $query = 
                    "
                        SELECT userId
                        FROM Log
                        WHERE activity = 'Received' AND documentId = '$documentId'
                        ORDER BY logId DESC
                    ";
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $currentHolderUserId = $row['userId'];
            
            // IDENTIFY IF THE ORIGIN IS THE CURRENT HOLDER OF THE DOCUMENTS
            if ($loggedUserId == $currentHolderUserId) {
                // GET CANCEL STEP ID
                $query = 
                    "
                        SELECT stepId
                        FROM RouteStep
                        WHERE routeId = '$routeId' AND subRouteId = 'CANCELTRAC' AND stepNumber = '1'
                    ";
            
                $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
                $row = mysql_fetch_array($result);
                $cancelStepId = $row['stepId'];
                
                // UPDATE MAIN DOCUMENT TABLE.
                $query = 
                    "
                        UPDATE MainDocument
                        SET currentStepId = '$cancelStepId', action = 'Sent'
                        WHERE mainDocumentId = '$documentId'
                    ";
                mysql_query($query);
                
                // CREATE RECORD FOR LOG TABLE (Cancelled MD)
                $query = 
                    "
                        INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                        VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Cancelled')
                    ";
                mysql_query($query);
                
                // CREATE RECORD FOR LOG TABLE (Sent MD)
                $query = 
                    "
                        INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                        VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                    ";
                mysql_query($query);
                
                // VERIFY IF THERE ARE ATTACHED ATTACHMENTS, IF YES, CREATE LOGS
                if ($attachedCount > 0) {
                    $attachedDocuments = explode(",", $attachedString);

                    for ($x=0; $x< $attachedCount;$x++) {
                        $query = 
                            "
                                INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                                VALUES ('$attachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                            ";
                        mysql_query($query);
                    }
                }
            }
            
            // ORIGIN IS NOT THE CURRENT HOLDER OF THE DOCUMENTS
            else {
                // UPDATE MAIN DOCUMENT TABLE.
                $query = 
                    "
                        UPDATE MainDocument
                        SET action = 'Cancelled'
                        WHERE mainDocumentId = '$documentId'
                    ";
                mysql_query($query);
                
                // CREATE RECORD FOR LOG TABLE (Cancelled MD)
                $query = 
                    "
                        INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                        VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Cancelled')
                    ";
                mysql_query($query);
                
            }
            
            // GET LOGID OF LATEST CANCEL
            $query = 
                    "
                        SELECT logId
                        FROM Log
                        WHERE activity = 'Cancelled' AND documentId = '$documentId'
                        ORDER BY logId DESC
                    ";
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $cancelLogId = $row['logId'];
            
            // THIS QUERY SAVES THE CANCELLATION MESSAGE TO LogMessage Table.
            $query = 
                "
                    INSERT INTO LogMessage (logId, message)
                    VALUES ('$cancelLogId', '$message')
                ";
            mysql_query($query);
            
            break;
        /**************************** CANCEL (END) ************************************/
        
        
        /************************ SEND FOR ARCHIVING (START) *******************************/
        /*
         * SEND (Cancelled)
         * $cancelStepId : Get current holder of the main document.
         * 
         * MainDocument table : activity = 'Sent'
         *                      currentStepId = '$cancelStepId'
         * 
         * Log table : Sent (Main Document)
         *             Sent (Attachments)
         */
        case "Send " : 
            
             // GET CANCEL STEP ID
            $query = 
                "
                    SELECT stepId
                    FROM RouteStep
                    WHERE routeId = '$routeId' AND subRouteId = 'CANCELTRAC' AND stepNumber = '1'
                ";

            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            $row = mysql_fetch_array($result);
            $cancelStepId = $row['stepId'];

            // UPDATE MAIN DOCUMENT TABLE.
            $query = 
                "
                    UPDATE MainDocument
                    SET currentStepId = '$cancelStepId', action = 'Sent'
                    WHERE mainDocumentId = '$documentId'
                ";
            mysql_query($query);
            
            // CREATE RECORD FOR LOG TABLE (Sent MD)
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                ";
            mysql_query($query);

            // VERIFY IF THERE ARE ATTACHED ATTACHMENTS, IF YES, CREATE LOGS
            if ($attachedCount > 0) {
                $attachedDocuments = explode(",", $attachedString);

                for ($x=0; $x< $attachedCount;$x++) {
                    $query = 
                        "
                            INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                            VALUES ('$attachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Sent')
                        ";
                    mysql_query($query);
                }
            }
            
            
            break;
            
        /************************ SEND FOR ARCHIVING (END) *******************************/
        
        

        
        
        
        
        
        
        
        
        /**************************** ARCHIVE (START) ************************************/
        /*
         * MainDocument table : status = 'Inactive', action = 'Archived'
         * Attachment table : status = 'Inactive' << ALL "attached" ATTACHMENTS FROM viewPageSending.php
         * Log table : Main Document and its attachments => Archived.
         * 
         */
        case "Archive" : 
            
            // UPDATE MAIN DOCUMENT TABLE.
            $query = 
                "
                    UPDATE MainDocument
                    SET status = 'Inactive', action = 'Archived'
                    WHERE mainDocumentId = '$documentId'
                ";
            mysql_query($query);
         
            // CREATE ARCHIVE LOG RECORD OF MAIN DOCUMENT
            $query = 
                "
                    INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                    VALUES ('$documentId', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Archived')
                ";
            mysql_query($query);
            
            // VERIFY IF THERE ARE ATTACHED ATTACHMENTS, IF YES, CREATE LOGS
            // ALSO, MAKE THE STATUS OF ATTACHMENTS INACTIVE.
            if ($attachedCount > 0) {
                $attachedDocuments = explode(",", $attachedString);

                for ($x=0; $x< $attachedCount;$x++) {
                    $query = 
                        "
                            INSERT INTO Log (documentId, userId, stepId, logDateTime, activity)
                            VALUES ('$attachedDocuments[$x]', '$loggedUserId', '$currentStepId', '$currentDateTime', 'Archived')
                        ";
                    mysql_query($query);
                    
                    $query = 
                        "
                            UPDATE Attachment
                            SET status = 'Inactive'
                            WHERE attachmentId = '$attachedDocuments[$x]'
                        ";
                    mysql_query($query);
                }
            }
            
            break;
        
            
        /**************************** ARCHIVE (END) ************************************/
        
    } // SWITCH - CASE END
    
    echo json_encode(1);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>